package self.hoy.demo.cloud.seata.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import self.hoy.demo.cloud.entities.CommonResult;
import self.hoy.demo.cloud.seata.entity.TOrder;
import self.hoy.demo.cloud.seata.service.OrderService;

import javax.annotation.Resource;

/**
 * @author 三共小不
 * @date 2020/6/20
 */
@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     *
     * @param order
     *      productId 奇数with全局事务
     *      count 偶数抛异常
     *      money
     * @return
     */
    @GetMapping("/order/create")
    public CommonResult create(TOrder order) {
        if (order != null && order.getProductId() % 2 == 1) {
            // @GlobalTransactional 理应回滚
            orderService.create1(order);
        } else {
            // without @GlobalTransactional 理应有不匹配的order、storage、account数据
            orderService.create2(order);
        }
        return new CommonResult(CommonResult.SUCCESS, "订单创建成功");
    }
}
